﻿Imports System.Data.OleDb

Public Class editprofile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM UserProfile WHERE UserID=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()

                If Not IsDBNull(dr("FirstName")) Then TB_FirstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then TB_LastName.Text = dr("LastName")
                If Not IsDBNull(dr("dateofbirth")) Then
                    Dim UserDOB As DateTime = dr("dateofbirth")
                    DDL_Days.SelectedValue = UserDOB.Day
                    DDL_Months.SelectedValue = UserDOB.Month
                    DDL_Years.SelectedValue = UserDOB.Year
                End If
                If Not IsDBNull(dr("picture")) Then
                    userProfileImage.ImageUrl = "~/Image/" + dr("picture")
                Else
                    userProfileImage.Visible = False
                End If

                If Not IsDBNull(dr("Gender")) Then RB_Gender.SelectedValue = dr("Gender")
                If Not IsDBNull(dr("qualifications")) Then TB_qualifications.Text = dr("qualifications")
                If Not IsDBNull(dr("specilaisation")) Then TB_specilaisation.Text = dr("specilaisation")
                If Not IsDBNull(dr("remark")) Then TB_remark.Text = dr("remark")
            End If
        End If

    End Sub

    Protected Sub bt_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BT_Submit.Click

        Dim SqlString As String
        Dim newFileName As String

        newFileName = Guid.NewGuid().ToString() & getSuffix(fup_RPicture.FileName)
        If fup_RPicture.HasFile Then
            fup_RPicture.SaveAs(Server.MapPath("Image") & "/" & newFileName)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        If fup_RPicture.HasFile Then

            SqlString = "UPDATE UserProfile SET FirstName=@f1, LastName=@f2, Picture=@f3, DateofBirth=@f4, Gender=@f5, qualifications=@f6, specilaisation=@f7 , remark=@f8 WHERE UserID=@f9"
        Else
            SqlString = "UPDATE UserProfile SET FirstName=@f1, LastName=@f2, DateofBirth=@f4, Gender=@f5, qualifications=@f6, specilaisation=@f7 , remark=@f8 WHERE UserID=@f9"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", TB_FirstName.Text)
        cmd.Parameters.AddWithValue("@f2", TB_LastName.Text)
        If fup_RPicture.HasFile Then cmd.Parameters.AddWithValue("@f3", newFileName)
        cmd.Parameters.AddWithValue("@f4", DDL_Days.SelectedValue & "/" & DDL_Months.SelectedValue & "/" & DDL_Years.SelectedValue)
        cmd.Parameters.AddWithValue("@f5", RB_Gender.SelectedValue)
        cmd.Parameters.AddWithValue("@f6", TB_qualifications.Text)
        cmd.Parameters.AddWithValue("@f7", TB_specilaisation.Text)
        cmd.Parameters.AddWithValue("@f8", TB_remark.Text)
        cmd.Parameters.AddWithValue("@f9", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("editprofile.aspx")

    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function


End Class